package model;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import model.HumiTempDao;

public class Listener extends Thread{
	private Socket socket;
	private DataInputStream inStream;
	private Scanner scannerIn;
	private DataOutputStream outStream;
	private HumiTempDao humitemp;
	private CamControl recorder;
	private boolean running = false;
	
	public void init(){
		try {
			recorder = new CamControl();
			humitemp = new HumiTempDao();
			//while loop om opnieuw verbinding te kunnen maken wanneer dk51 uitvalt
			running = true;
			while(running){
				System.out.println("waiting for connection");
				socket = new ServerSocket(3333).accept();
				System.out.println("connection established");
				inStream = new DataInputStream(socket.getInputStream());
				scannerIn = new Scanner(inStream);
				outStream = new DataOutputStream(socket.getOutputStream());
				running = true;
				standBy();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void run() {
		init();
	}
	
	
	public void end(){
		running = false;
	}
	
	public void changeInterval(String i){
		try {
			if(outStream != null){
				outStream.writeBytes(i);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void standBy(){
		try{
			System.out.println("standing by...");
			while(!socket.isInputShutdown()){
				if(inStream.available() > 0){
					System.out.println("going to read line...");
					String line = scannerIn.nextLine();
					System.out.println("received: " + line);
					switch(line.charAt(0)){
						case 'T': addTemp(line);break;
						case 'H': addHumi(line);break;
						case 'C': doRecord(line);break;
						default : System.out.println("unknown type recieved");break;
					}
				}else{
					Thread.sleep(1000);
				}
			}
			System.out.println("closing...");
			scannerIn.close();
			try {
				inStream.close();
				outStream.close();
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}catch(IOException e){
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doRecord(String line) {
		String actie = line.split("=")[1];
		if(actie.toLowerCase().equals("1")){
			recorder.record();
		}else if(actie.toLowerCase().equals("0")){
			recorder.stopRecord();
		}
	}

	private void addHumi(String line) {
		double humi = new Double(line.split("=")[1]);
		humitemp.addHumi(humi);
	}

	private void addTemp(String line){
		double temp = new Double(line.split("=")[1]);
		humitemp.addTemp(temp);
	}
}
